package com.monkeybrother.thread.atomic;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Desc
 * @Author houry
 * @Date 2021/1/22 15:41
 **/
public class ABATest {
    private final static AtomicInteger num = new AtomicInteger(100);

    public static void main(String[] args) {
        new Thread(() -> {
            num.compareAndSet(100, 101);
            num.compareAndSet(101, 100);
            System.out.println(Thread.currentThread().getName() + " 修改num之后的值：" + num.get());
        }).start();

        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
                num.compareAndSet(100, 200);
                System.out.println(Thread.currentThread().getName() + " 修改num之后的值：" + num.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
